package com.base.animation.item

import android.graphics.Canvas
import android.graphics.Paint
import android.support.annotation.CallSuper
import com.base.animation.IDisplayItem
import java.util.concurrent.atomic.AtomicLong

/**
 * @author:zhouzechao
 * @date: 2020/12/8
 * description：bitmap的display模块
 */
abstract class BaseDisplayItem @JvmOverloads constructor(paint: Paint) : IDisplayItem {

    open var displayHeight: Int = 0
    open var displayWidth: Int = 0
    private val itemId = animDisplayId.incrementAndGet()

    override var displayItemId: String = itemId.toString()
        set(value) {
            field = value
        }

    override var width: Int = 0

    override var height: Int = 0

    protected val mPaint: Paint = paint

    override fun attachView(width: Int, height: Int) {
        this.width = width
        this.height = height
    }

    override fun draw(
        canvas: Canvas, x: Float, y: Float, alpha: Int, scaleX: Float, scaleY: Float,
        rotation: Float
    ) {
        canvas.save()
        canvas.scale(scaleX, scaleY, x + getScalePX(scaleX), y + getScalePY(scaleY))
        canvas.rotate(rotation, x + getRotatePX(rotation, scaleX),
            y + getRotatePY(rotation, scaleY))

        draw(canvas, x, y, alpha, scaleX, scaleY)

        canvas.restore()
    }

    @CallSuper
    override fun release() {
        displayHeight = 0
        displayWidth = 0
    }

    override fun setDisplaySize(displayWidth: Int, displayHeight: Int) {
        this.displayWidth = displayWidth
        this.displayHeight = displayHeight
    }

    override fun getScalePX(scaleX: Float): Float {
        return 0f
    }

    override fun getScalePY(scaleY: Float): Float {
        return 0f
    }

    override fun getRotatePX(rotation: Float, scaleX: Float): Float {
        return 0f
    }

    override fun getRotatePY(rotation: Float, scaleY: Float): Float {
        return 0f
    }

    abstract fun draw(canvas: Canvas, x: Float, y: Float, alpha: Int, scaleX: Float, scaleY: Float)
}

val animDisplayId = AtomicLong(System.currentTimeMillis() / 1000L)